/**  
 * @Title: CmpCaseService.java
 * @Description: TODO(描述)
 * @author liuyi
 * @date 2023-05-06 14:23:02 
 */  
package com.fsxgt.datagrid.component.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fsxgt.datagrid.component.bo.CmpCaseBO;
import com.fsxgt.datagrid.component.bo.CmpCaseRouteBO;
import com.fsxgt.datagrid.component.dao.CmpCaseMapper;
import com.fsxgt.datagrid.component.dao.CmpCaseRouteMapper;
import com.fsxgt.datagrid.flow.bo.FlowComponentBO;
import com.fsxgt.datagrid.flow.dao.FlowInfoMapper;
import com.fsxgt.datagrid.flow.service.IComponentService;

import lombok.extern.slf4j.Slf4j;

/**  
 * @ClassName: CmpCaseService
 * @Description: TODO(描述)
 * @author liuyi
 * @date 2023-09-07 08:23:02 
*/
@Slf4j
@Service
public class CmpCaseService implements IComponentService{
	

	
	@Autowired
	private CmpCaseMapper cmpCaseMapper;
	@Autowired
	private CmpCaseRouteMapper cmpCaseRouteMapper;
	@Autowired
	private FlowInfoMapper flowInfoMapper;
	
	
	
	@Transactional(rollbackFor = Exception.class)
	public void saveCmpCase(CmpCaseBO cmpCaseBO) {
		CmpCaseBO bo = cmpCaseMapper.get(cmpCaseBO.getFlowInfoId(),cmpCaseBO.getComponentSeq());
		if(bo==null) {
			cmpCaseMapper.insert(cmpCaseBO);
		}else {
			cmpCaseMapper.update(cmpCaseBO);
		}
		
		for(CmpCaseRouteBO route:cmpCaseBO.getRouteList()) {
			route.setComponentSeq(cmpCaseBO.getComponentSeq());
			route.setFlowInfoId(cmpCaseBO.getFlowInfoId());
		}
		cmpCaseRouteMapper.delete(cmpCaseBO.getFlowInfoId(),cmpCaseBO.getComponentSeq());

		for(int i=0;i<cmpCaseBO.getRouteList().size();i++) {
			cmpCaseBO.getRouteList().get(i).setSeq(i+1);
		}

		
		if(cmpCaseBO.getRouteList().size()>0) {
			cmpCaseRouteMapper.insert(cmpCaseBO.getRouteList());
		}
		
		flowInfoMapper.updateFlowComponentName(cmpCaseBO.getName(), cmpCaseBO.getFlowInfoId(), cmpCaseBO.getComponentSeq());

		
	}
	@Transactional(rollbackFor = Exception.class)
	public void deleteComponent(CmpCaseBO cmpCaseBO) {
		deleteComponent(cmpCaseBO.getFlowInfoId(),cmpCaseBO.getComponentSeq());
	}
	/**  
	 * @Title: deleteComponent
	 * @Description: TODO(描述)
	 * @param flowInfoId
	 * @param seq 
	 * @see com.fsxgt.datagrid.core.service.impl.IComponentService#deleteComponent(java.lang.String, java.lang.String) 
	 * @author liuyi
	 * @date 2023-05-08 09:08:12 
	 */ 
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteComponent(String flowInfoId, String seq) {
		// TODO Auto-generated method stub
		cmpCaseMapper.delete(flowInfoId,seq);
		cmpCaseRouteMapper.delete(flowInfoId,seq);
	}
	/**  
	 * @Title: getComponentType
	 * @Description: TODO(描述)
	 * @return 
	 * @see com.fsxgt.datagrid.core.service.impl.IComponentService#getComponentType() 
	 * @author liuyi
	 * @date 2023-05-08 09:08:12 
	 */ 
	@Override
	public String getComponentType() {
		// TODO Auto-generated method stub
		return "Case";
	}
	/**  
	 * @Title: getCase
	 * @Description: TODO(描述)
	 * @param bo
	 * @author liuyi
	 * @date 2023-05-08 09:51:14 
	 */  
	public CmpCaseBO getCase(CmpCaseBO cmpCaseBO) {
		// TODO Auto-generated method stub
		CmpCaseBO bo = cmpCaseMapper.get(cmpCaseBO.getFlowInfoId(),cmpCaseBO.getComponentSeq());
		if(bo!=null) {
			List<CmpCaseRouteBO> list = cmpCaseRouteMapper.getList(cmpCaseBO.getFlowInfoId(),cmpCaseBO.getComponentSeq());
			bo.setRouteList(list);
		}
		return bo;
	}
	/**  
	 * @Title: getConfig
	 * @Description: TODO(描述)
	 * @param flowId
	 * @param seq
	 * @return 
	 * @see com.fsxgt.datagrid.core.service.IComponentService#getConfig(java.lang.String, java.lang.String) 
	 * @author liuyi
	 * @date 2023-05-25 15:24:47 
	 */ 
	@Override
	public Object getConfig(String flowId, String seq) {
		// TODO Auto-generated method stub
		
		CmpCaseBO bo = new CmpCaseBO();
		bo.setFlowInfoId(flowId);
		bo.setComponentSeq(seq);
		
		return getCase(bo);
	}
	/**  
	 * @Title: findNodes
	 * @Description: TODO(描述)
	 * @param string
	 * @return
	 * @author liuyi
	 * @date 2023-09-07 09:21:24 
	 */  
	public List findNodes(String flowInfoId) {
		// TODO Auto-generated method stub

		List<FlowComponentBO> list = flowInfoMapper.findFlowComponentsByFlowId(flowInfoId);

		List<Map> selectList = list.stream().map(bo -> {
			Map<String, Object> apiMap = new HashMap<>();
			apiMap.put("value", bo.getComponentSeq());
			apiMap.put("name", bo.getName());
			return apiMap;
		}).collect(Collectors.toList());

		return selectList;
	}
}
